package com.example.micro_property_springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.micro_property_springboot.Exception.AssetCategoryNotFoundException;
import com.example.micro_property_springboot.Exception.IllegalParamException;
import com.example.micro_property_springboot.pojo.AssetCategory;
import com.example.micro_property_springboot.pojo.DTO.request.AssetCategoryAddDTO;
import com.example.micro_property_springboot.pojo.DTO.request.AssetCategoryQueryDTO;
import com.example.micro_property_springboot.pojo.VO.R;
import com.example.micro_property_springboot.service.AssetCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资产分类Controller
 */
@RestController
@RequestMapping("/api/asset-category")
@Tag(name = "资产分类接口", description = "资产分类接口")
public class AssetCategoryController {

    @Autowired
    private AssetCategoryService assetCategoryService;

    /**
     * 多条件分页查询
     * 示例：/api/asset-category/page?name=办公设备&parentId=1&pageNum=1&pageSize=20
     */
    @GetMapping("/page")
    @Operation(summary = "多条件分页查询资产分类")
    public R<Page<AssetCategory>> queryCategoryPage(
            @Valid AssetCategoryQueryDTO queryDTO, // 校验查询参数
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Page<AssetCategory> page = assetCategoryService.queryCategoryPage(queryDTO, pageNum, pageSize);
        return R.success(page);
    }

    /**
     * 单个编辑资产分类
     */
    @PutMapping
    @Operation(summary = "单个编辑资产分类")
    public R<AssetCategory> updateCategory(@Valid @RequestBody AssetCategory category) {
        AssetCategory updated = assetCategoryService.updateCategory(category);
        return R.success(updated);
    }

    /**
     * 批量编辑资产分类
     */
    @PutMapping("/batch")
    @Operation(summary = "批量编辑资产分类")
    public R<Integer> batchUpdateCategories(@Valid @RequestBody List<AssetCategory> categories) {
        int updateCount = assetCategoryService.batchUpdateCategories(categories);
        return R.success(updateCount);
    }

    /**
     * 单个删除资产分类
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "单个删除资产分类")
    public R<Boolean> deleteCategory(@PathVariable Long id) {
        boolean success = assetCategoryService.deleteCategory(id);
        return R.success(success);
    }

    /**
     * 批量删除资产分类
     */
    @DeleteMapping("/batch")
    @Operation(summary = "批量删除资产分类")
    public R<Integer> batchDeleteCategories(@RequestBody List<Long> ids) {
        int deleteCount = assetCategoryService.batchDeleteCategories(ids);
        return R.success(deleteCount);
    }

    @PostMapping
    @Operation(summary = "新增资产分类")
    public R<AssetCategory> addCategory(@Valid @RequestBody AssetCategoryAddDTO addDTO) {
        AssetCategory addedCategory = assetCategoryService.addCategory(addDTO);
        return R.success(addedCategory);
    }

    /**
     * 全局异常处理（返回友好提示）
     */
    @ExceptionHandler({AssetCategoryNotFoundException.class, IllegalParamException.class})

    public R<Void> handleBusinessException(RuntimeException e) {
        return R.fail(400, e.getMessage());
    }

    @ExceptionHandler(IllegalArgumentException.class)
    @Operation(summary = "参数异常处理")
    public R<Void> handleIllegalArgException(IllegalArgumentException e) {
        return R.fail(400, e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @Operation(summary = "系统异常处理")
    public R<Void> handleException(Exception e) {
        e.printStackTrace(); // 生产环境替换为日志框架（如SLF4J）
        return R.fail("系统异常，请联系管理员");
    }
}
